package ch11_11_4;

public class ch11_11_4 {
	public static void main(String[] args) {
		new ch11_11_4();
	}

	public ch11_11_4() {
		// Define the arrays to be tested.
		Student[] s = new Student[3];
		s[0] = new Student(new Name("Derek", 'S', "Dexony"));
		s[1] = new Student(new Name("Stacy", 'M', "Waters"));
		s[2] = new Student(new Name("Adamo", 'U', "Leetmz"));

		String[] str = { "Orange", "Jackblade", "Apple" };
		Integer[] i = { new Integer(5), new Integer(0), new Integer(3) };

		// Display current array of students
		printList(s);
		System.out.println();

		// Display max of students
		System.out.println("Max is " + max(s));
		System.out.println();

		// Display sorted students
		sort(s);
		printList(s);
		System.out.println();

		// Display current array of strings
		printList(str);
		System.out.println();

		// Display max of strings
		System.out.println("Max is " + max(str));
		System.out.println();

		// Display sorted strings
		sort(str);
		printList(str);
		System.out.println();

		// Display current array of integers
		printList(i);
		System.out.println();

		// Display max of integers
		System.out.println("Max is " + max(i));
		System.out.println();

		// Display sorted integers
		sort(i);
		printList(i);
	}

	/** Print an array of objects */
	public static void printList(Object[] object) {
		for (int i = 0; i < object.length; i++) {
			System.out.println(object[i]);
		}
	}

	@SuppressWarnings("unchecked")
	public static Object max(Object[] object) {
		// Find max of array object
		Object maxOfA = object[0];
		for (int i = 1; i < object.length; i++) {
			if ((((Comparable<Object>) maxOfA).compareTo(object[i])) == -1) {
				maxOfA = object[i];
			}
		}

		// Return results.
		return maxOfA;
	}

	@SuppressWarnings("unchecked")
	public static void sort(Object[] object) {
		// Set the variables for the current minObject, minIndex, and compare value
		Object currentMin;
		int currentMinIndex;
		int compareInt;

		// Begin loop to work through the list.
		for (int i = 0; i < object.length; i++) {
			// Find the minimum in the list.
			currentMin = object[i];
			currentMinIndex = i;
			for (int k = i + 1; k < object.length; k++) {
				compareInt = ((Comparable) currentMin).compareTo(object[k]);
				if (compareInt > 0) {
					currentMin = object[k];
					currentMinIndex = k;
				}
			}

			// Swap list[i] with list[currentMaxIndex] if needed.
			if (currentMinIndex != i) {
				object[currentMinIndex] = object[i];
				object[i] = currentMin;
			}
		}
	}
}

